<?php

namespace App\Console\Commands;

use App\Models\Tools\Data;
use App\Models\Tools\Period;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class ExploreCheck extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:explore_check';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '已验收工单公示结束待结算';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        $error_works = [];
        try{
            // 计算当前时间的超时时间
            $overtimes = Period::getSetting('EXPLORE_CHECK_TIME');
            $time = date('Y-m-d H:i:s',time()-$overtimes);

            // 查询所有已验收工单
            $work_ids = DB::table('explore_work')
                ->where('status','=',Data::DATA_DB_EXPLORE_WORK_STATUS_ACCEPTED)
                ->where('submit_time','<',$time)
                ->pluck('work_id')
                ->toArray();

            // 如果存在待修改状态的工单，自动处理
            if(count($work_ids)){
                Log::info('command:explore_check',['time'=>$time,'work_ids'=>$work_ids]);
                foreach ($work_ids as $work_id) {
                    $result = self::checkWorks($work_id);
                    if(!$result){
                        $error_works[] = $work_id;
                    }
                }
            }
        }catch (\Exception $e){
            Log::error(ExploreCheck::class,['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine(), 'error_works' => $error_works]);
        }
        return;
    }

    /**
     * 验收工单结束进入待结算
     * @param int $work_id 工单id
     * @return bool 成功或失败
     */
    public static function checkWorks(int $work_id):bool
    {
        try{
            DB::beginTransaction();
            $status = DB::table('explore_work')
                ->where('work_id','=',$work_id)
                ->lockForUpdate()
                ->value('status');
            if(!$status === Data::DATA_DB_EXPLORE_WORK_STATUS_ACCEPTED){
                DB::rollBack();
                Log::error('自动修改工单状态时数据被修改',['work_id'=>$work_id,'status'=>$status]);
                return false;
            }
            DB::table('explore_work')
                ->where('work_id','=',$work_id)
                ->update(['status' => Data::DATA_DB_EXPLORE_WORK_STATUS_CHECK]);
            DB::table('explore_work_log')->insert([
                'work_id' => $work_id,
                'action' => Data::DATA_DB_EXPLORE_WORK_ACTION_CHECK,
                'remark' => '工单公示时间到期，进入待结算阶段',
            ]);
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            Log::error(ExploreCheck::class,['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
            return false;
        }
        return true;
    }
}
